#!/usr/bin/env python
# -*- coding: utf-8 -*-

from cta.config.cross_section_config.multi_factor_config import MultiFactorConfig
from web.models import CommodityFutureInfo
from web.service.base_service import BaseService
from web.manager.log_manager import LogManager

Logger = LogManager.get_logger(__name__)

class CommodityFutureAnalysisDetailService(BaseService):
    """
    CommodityFutureAnalysisDetail的service类
    """

    def write_by_code(self):
        """
        海量地向c_f_analysis_detail表插入数据
        """

        Logger.info("开始海量地向c_f_analysis_detail表插入数据")

        # variance_type字段列表
        variance_type_list: list[str] = list()
        variance_type_list.append("variance20")
        variance_type_list.append("variance60")
        variance_type_list.append("variance120")

        # variance_value百分比列表
        variance_value_percentage_list: list[float] = list()
        # variance_value_percentage_list.append(0.1)
        # variance_value_percentage_list.append(0.2)
        # variance_value_percentage_list.append(0.3)
        variance_value_percentage_list.append(2)
        variance_value_percentage_list.append(1.75)
        variance_value_percentage_list.append(1.5)
        variance_value_percentage_list.append(1.25)
        variance_value_percentage_list.append(1)
        variance_value_percentage_list.append(0.75)
        variance_value_percentage_list.append(0.5)
        variance_value_percentage_list.append(0.25)

        # ma字段列表
        ma_type_list: list[str] = list()
        ma_type_list.append("ma60")
        ma_type_list.append("ma120")
        ma_type_list.append("ma250")

        # ma字段回溯列表
        ma_backtrack_list: list[int] = list()
        # ma_backtrack_list.append(5)
        # ma_backtrack_list.append(10)
        # ma_backtrack_list.append(20)
        ma_backtrack_list.append(1)

        # 查询全部期货信息
        commodity_future_info_list: list[CommodityFutureInfo] = self.commodity_future_info_dao.find_by_code("SR")
        # commodity_future_info_list: list[CommodityFutureInfo] = self.commodity_future_info_dao.find_all()

        if commodity_future_info_list is not None and len(commodity_future_info_list) > 0:
            for commodity_future_info in commodity_future_info_list:

                # 如果是被排出的期货，则跳过
                if commodity_future_info.code in list(map(lambda x: x, MultiFactorConfig.Exclude_List)):
                    Logger.info("期货[%s]不参与本次计算", commodity_future_info.code)
                    continue

                # 查询最大方差
                # variance20, variance60, variance120 = self.commodity_future_date_data_dao.find_max_variance20_60_120_by_code(commodity_future_info.code)
                variance20, variance60, variance120 = self.commodity_future_date_data_dao.find_average_variance20_60_120_by_code(commodity_future_info.code)

                # 计算方差阈值
                variance_value_list: list[float] = list()
                # variance20_10_percentage = float(variance20) * variance_value_percentage_list[0]
                # variance20_20_percentage = float(variance20) * variance_value_percentage_list[1]
                # variance20_30_percentage = float(variance20) * variance_value_percentage_list[2]
                # variance60_10_percentage = float(variance60) * variance_value_percentage_list[0]
                # variance60_20_percentage = float(variance60) * variance_value_percentage_list[1]
                # variance60_30_percentage = float(variance60) * variance_value_percentage_list[2]
                # variance120_10_percentage = float(variance120) * variance_value_percentage_list[0]
                # variance120_20_percentage = float(variance120) * variance_value_percentage_list[1]
                # variance120_30_percentage = float(variance120) * variance_value_percentage_list[2]
                # variance_value_list.append(variance20_10_percentage)
                # variance_value_list.append(variance20_20_percentage)
                # variance_value_list.append(variance20_30_percentage)
                # variance_value_list.append(variance60_10_percentage)
                # variance_value_list.append(variance60_20_percentage)
                # variance_value_list.append(variance60_30_percentage)
                # variance_value_list.append(variance120_10_percentage)
                # variance_value_list.append(variance120_20_percentage)
                # variance_value_list.append(variance120_30_percentage)
                variance20_2_percentage = float(variance20) * variance_value_percentage_list[0]
                variance20_1_75_percentage = float(variance20) * variance_value_percentage_list[1]
                variance20_1_5_percentage = float(variance20) * variance_value_percentage_list[2]
                variance20_1_25_percentage = float(variance20) * variance_value_percentage_list[3]
                variance20_1_percentage = float(variance20) * variance_value_percentage_list[4]
                variance20_0_75_percentage = float(variance20) * variance_value_percentage_list[5]
                variance20_0_5_percentage = float(variance20) * variance_value_percentage_list[6]
                variance20_0_25_percentage = float(variance20) * variance_value_percentage_list[7]
                variance60_2_percentage = float(variance60) * variance_value_percentage_list[0]
                variance60_1_75_percentage = float(variance60) * variance_value_percentage_list[1]
                variance60_1_5_percentage = float(variance60) * variance_value_percentage_list[2]
                variance60_1_25_percentage = float(variance60) * variance_value_percentage_list[3]
                variance60_1_percentage = float(variance60) * variance_value_percentage_list[4]
                variance60_0_75_percentage = float(variance60) * variance_value_percentage_list[5]
                variance60_0_5_percentage = float(variance60) * variance_value_percentage_list[6]
                variance60_0_25_percentage = float(variance60) * variance_value_percentage_list[7]
                variance120_2_percentage = float(variance120) * variance_value_percentage_list[0]
                variance120_1_75_percentage = float(variance120) * variance_value_percentage_list[1]
                variance120_1_5_percentage = float(variance120) * variance_value_percentage_list[2]
                variance120_1_25_percentage = float(variance120) * variance_value_percentage_list[3]
                variance120_1_percentage = float(variance120) * variance_value_percentage_list[4]
                variance120_0_75_percentage = float(variance120) * variance_value_percentage_list[5]
                variance120_0_5_percentage = float(variance120) * variance_value_percentage_list[6]
                variance120_0_25_percentage = float(variance120) * variance_value_percentage_list[7]
                variance_value_list.append(variance20_2_percentage)
                variance_value_list.append(variance20_1_75_percentage)
                variance_value_list.append(variance20_1_5_percentage)
                variance_value_list.append(variance20_1_25_percentage)
                variance_value_list.append(variance20_1_percentage)
                variance_value_list.append(variance20_0_75_percentage)
                variance_value_list.append(variance20_0_5_percentage)
                variance_value_list.append(variance20_0_25_percentage)
                variance_value_list.append(variance60_2_percentage)
                variance_value_list.append(variance60_1_75_percentage)
                variance_value_list.append(variance60_1_5_percentage)
                variance_value_list.append(variance60_1_25_percentage)
                variance_value_list.append(variance60_1_percentage)
                variance_value_list.append(variance60_0_75_percentage)
                variance_value_list.append(variance60_0_5_percentage)
                variance_value_list.append(variance60_0_25_percentage)
                variance_value_list.append(variance120_2_percentage)
                variance_value_list.append(variance120_1_75_percentage)
                variance_value_list.append(variance120_1_5_percentage)
                variance_value_list.append(variance120_1_25_percentage)
                variance_value_list.append(variance120_1_percentage)
                variance_value_list.append(variance120_0_75_percentage)
                variance_value_list.append(variance120_0_5_percentage)
                variance_value_list.append(variance120_0_25_percentage)

                for variance_type in variance_type_list:
                    for variance_value in variance_value_list:
                        for ma_type in ma_type_list:
                            for ma_backtrack in ma_backtrack_list:
                                Logger.info("海量地向c_f_analysis_detail表插入数据，variance_type为[%s]，variance_value为[%s]，ma_type为[%s]，ma_backtrack为[%s]，code为[%s]",
                                            variance_type, variance_value, ma_type, ma_backtrack, commodity_future_info.code)
                                self.commodity_future_analysis_detail_dao.write_by_code(variance_type, variance_value, ma_type, ma_backtrack, commodity_future_info.code)
